//
//  UINavigationController+Safe.m
//  niuguwang
//
//  Created by BrightLi on 2021/7/19.
//  Copyright © 2021年 taojinzhe. All rights reserved.
//

#import "UINavigationController+Safe.h"
#import "YMSafeUtil.h"
#import <objc/runtime.h>

@implementation UINavigationController(Safe)

static const void *navStackLastChangedTimeKey=&navStackLastChangedTimeKey;

+ (void) safe_fixed
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [YMSafeUtil swizzleInstance:[self class]
                  sel1:@selector(pushViewController:animated:)
                  sel2:@selector(safe_pushViewController:animated:)];
        [YMSafeUtil swizzleInstance:[self class]
                  sel1:@selector(dismissViewControllerAnimated:completion:)
                  sel2:@selector(safe_dismissViewControllerAnimated:completion:)];
        [YMSafeUtil swizzleInstance:[self class]
                  sel1:@selector(popViewControllerAnimated:)
                  sel2:@selector(safe_popViewControllerAnimated:)];
        [YMSafeUtil swizzleInstance:[self class]
                  sel1:@selector(setNavigationBarHidden:animated:)
                  sel2:@selector(safe_setNavigationBarHidden:animated:)];
    });
}

- (NSTimeInterval) navStackLastChangedTime
{
    NSTimeInterval time = [objc_getAssociatedObject(self, navStackLastChangedTimeKey) doubleValue];
    return time;
}

- (void) setNavStackLastChangedTime:(NSTimeInterval)navStackLastChangedTime
{
    objc_setAssociatedObject(self, navStackLastChangedTimeKey, @(navStackLastChangedTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(void) safe_pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [self safe_pushViewController:viewController animated:animated];
}

- (UIViewController *) safe_popViewControllerAnimated:(BOOL)animated;
{
    return [self safe_popViewControllerAnimated:animated];
}

- (void) safe_dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
    [self safe_dismissViewControllerAnimated:flag completion:completion];
}

-(void) safe_setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated;
{
    [self safe_setNavigationBarHidden:hidden animated:animated];
}

@end

@implementation UINavigationItem(Safe)

+ (void) safe_fixed
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [YMSafeUtil swizzleInstance:[self class]
                  sel1:@selector(initWithRootViewController:)
                  sel2:@selector(safe_initWithRootViewController:)];
        [YMSafeUtil swizzleInstance:[self class]
                  sel1:@selector(presentModalViewController:animated:)
                  sel2:@selector(safe_presentModalViewController:animated:)];
        [YMSafeUtil swizzleInstance:[self class]
                  sel1:@selector(setLeftBarButtonItem:)
                  sel2:@selector(safe_setLeftBarButtonItem:)];
        [YMSafeUtil swizzleInstance:[self class]
                  sel1:@selector(setRightBarButtonItem:)
                  sel2:@selector(safe_setRightBarButtonItem:)];
        [YMSafeUtil swizzleInstance:[self class]
                  sel1:@selector(setBackBarButtonItem:)
                  sel2:@selector(safe_setBackBarButtonItem:)];
        [YMSafeUtil swizzleInstance:[self class]
                  sel1:@selector(setTitleView:)
                  sel2:@selector(safe_setTitleView:)];
        
    });
}

- (instancetype) safe_initWithRootViewController:(UIViewController *)rootViewController
{
    return [self safe_initWithRootViewController:rootViewController];
}

- (void) safe_presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
{
    [self safe_presentModalViewController:modalViewController animated:animated];
}

- (void) safe_setBackBarButtonItem:(UIBarButtonItem *)backBarButtonItem
{
    [self safe_setBackBarButtonItem:backBarButtonItem];
}

- (void) safe_setLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem
{
    [self safe_setLeftBarButtonItem:leftBarButtonItem];
}

- (void) safe_setRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem
{
    [self safe_setRightBarButtonItem:rightBarButtonItem];
}

- (void) safe_setTitleView:(UIView *)titleView
{
    [self safe_setTitleView:titleView];
}

@end

